package com.changhong.autopairservice.core;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.ParcelUuid;
import android.util.Log;

import com.changhong.autopairservice.utils.Common;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@SuppressLint({"NewApi"})
public class InputDeviceProfile implements BluetoothProfile.ServiceListener {
    private static final int DEVICE_TYPE_CLASSIC = 1;
    private static final int INPUT_DEVICE_PROFILE = 4; // BluetoothProfile.INPUT_DEVICE
    private static final int CONNECTED_STATE = 2;
    
    // HID UUID for Bluetooth Input Device Profile
    private static final ParcelUuid HID_UUID = ParcelUuid.fromString("00001124-0000-1000-8000-00805F9B34FB");
    
    private static final String TAG = "InputDeviceProfile";
    
    private boolean isReady;
    private BluetoothProfile mInputDevice;

    public InputDeviceProfile(Context context) {
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, this, INPUT_DEVICE_PROFILE);
    }

    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        this.mInputDevice = proxy;
        this.isReady = true;
    }

    public void onServiceDisconnected(int profile) {
        this.mInputDevice = null;
        this.isReady = false;
    }

    public boolean isReady() {
        return this.isReady;
    }

    public boolean connect(BluetoothDevice device) {
        if (this.mInputDevice == null) {
            return false;
        }
        
        ParcelUuid[] uuids = device.getUuids();
        if (uuids == null) {
            return false;
        }
        
        // Check if the device supports HID profile
        boolean isHidDevice = false;
        for (ParcelUuid uuid : uuids) {
            if (HID_UUID.equals(uuid)) {
                isHidDevice = true;
                break;
            }
        }
        
        if (!isHidDevice) {
            return false;
        }
        
        try {
            // Try to use reflection to call the connect method (hidden API)
            Method connectMethod = this.mInputDevice.getClass().getMethod("connect", BluetoothDevice.class);
            if (connectMethod != null) {
                return (Boolean) connectMethod.invoke(this.mInputDevice, device);
            } else {
                Log.w(TAG, "connect method not found in BluetoothProfile");
                return false;
            }
        } catch (Exception e) {
            // If reflection fails, return false
            Log.w(TAG, "Failed to connect using reflection: " + e.getClass().getSimpleName() + " - " + e.getMessage());
            return false;
        }
    }

    public boolean disconnect(BluetoothDevice device) {
        if (this.mInputDevice == null) {
            return false;
        }
        
        try {
            // Try to use reflection to call the disconnect method (hidden API)
            Method disconnectMethod = this.mInputDevice.getClass().getMethod("disconnect", BluetoothDevice.class);
            if (disconnectMethod != null) {
                return (Boolean) disconnectMethod.invoke(this.mInputDevice, device);
            } else {
                Log.w(TAG, "disconnect method not found in BluetoothProfile");
                return false;
            }
        } catch (Exception e) {
            // If reflection fails, return false
            Log.w(TAG, "Failed to disconnect using reflection: " + e.getClass().getSimpleName() + " - " + e.getMessage());
            return false;
        }
    }

    public int getConnectionStatus(BluetoothDevice device) {
        if (this.mInputDevice == null) {
            return 0;
        }
        List<BluetoothDevice> deviceList;
        int i;
        try {
            deviceList = this.mInputDevice.getConnectedDevices();
        } catch (Exception e) {
            deviceList = new ArrayList();
        }
        if (deviceList.isEmpty() || !((BluetoothDevice) deviceList.get(0)).equals(device)) {
            i = 0;
        } else {
            i = this.mInputDevice.getConnectionState(device);
        }
        return i;
    }

    protected void finalize() throws Throwable {
        super.finalize();
        release();
    }

    public void release() {
        if (this.mInputDevice != null) {
            BluetoothAdapter.getDefaultAdapter().closeProfileProxy(INPUT_DEVICE_PROFILE, this.mInputDevice);
        }
        this.mInputDevice = null;
        this.isReady = false;
    }
}